package com.cykj.eurekaclient.utils.news;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.net.URLEncoder;

public class NewsTool {
    public static final String APP_KEY = "your_appkey_here";// 你的appKey
    public static final String URL = "https://api.jisuapi.com/news/get";
    public static final String channel = "头条";// utf8  新闻频道(头条,财经,体育,娱乐,军事,教育,科技,NBA,股票,星座,女性,健康,育儿)
    public static final int num = 40;// 数量 默认10，最大40

    public static void NewGet() throws Exception {
        String result = null;
        String url = URL + "?channel=" + URLEncoder.encode(channel, "utf-8") + "&num=" + num + "&appkey=" + APP_KEY;

        try {
            result = HttpUtil.sendGet(url, "utf-8");
            JSONObject json = JSONObject.fromObject(result);
            if (json.getInt("status") != 0) {
                System.out.println(json.getString("msg"));
            } else {
                JSONObject resultArr = (JSONObject) json.opt("result");
                String channel = resultArr.getString("channel");
                String num = resultArr.getString("num");
                System.out.println(channel + " " + num);
                JSONArray list = resultArr.optJSONArray("list");
                for (int i = 0; i < list.size(); i++) {
                    JSONObject obj = (JSONObject) list.opt(i);
                    String title = obj.getString("title");
                    String time = obj.getString("time");
                    String src = obj.getString("src");
                    String category = obj.getString("category");
                    String pic = obj.getString("pic");
                    String content = obj.getString("content");
                    String url1 = obj.getString("url");
                    String webUrl = obj.getString("weburl");
                    System.out.println(title + " " + time + " " + src + " " + category + " " + pic + " " + content + " "
                            + url1 + " " + webUrl);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
